﻿using System;
using EmbeddedSharp.Core;

namespace EmbeddedSharp.Scripting {
    public class FunctionReferenceNode : ContextFuncNode {
        public FunctionReferenceNode()
            : base() {
            throw new ArgumentNullException("Please specify a name for the referenced one.");
        }

        public FunctionReferenceNode(string value)
            : base(value) {
        }

        public override object Evaluate() {
            FuncNode actualFuncNode = Context.GetVariable(Value) as FuncNode;
            if (actualFuncNode == null) {
                throw new FunctionNotDefinedException(Value);
            }
            foreach (Node arg in Parameters) {
                actualFuncNode.Parameters.Add(arg);
            }

            object ret = actualFuncNode.Evaluate();

            return ret;
        }
    }
}
